///*
// * To change this template, choose Tools | Templates
// * and open the template in the editor.
// */
//package company.search.beans;
//
//import abstractClasses.AbstractEntityCreator;
//import company.folder.qualifiers.SeeAllProfilesEvent;
//import facades.CandidateFacade;
//import facades.CompanyAdquiredProfileFacade;
//import facades.CompanyAdquiredProfileFolderFacade;
//import facades.CompanyVisitedProfileFacade;
//import facades.SearchFacade;
//import entities.Candidate;
//import entities.Company;
//import entities.CompanyAdquiredProfile;
//import entities.CompanyAdquiredProfileFolder;
//import entities.CompanyFolder;
//import entities.CompanyVisitedProfile;
//import entities.Search;
//import javax.inject.Named;
//import java.io.Serializable;
//import java.math.BigDecimal;
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//import javax.enterprise.context.SessionScoped;
//import javax.enterprise.event.Event;
//import javax.enterprise.event.Observes;
//import javax.inject.Inject;
//import qualifiers.CreatedEntityEvent;
//import qualifiers.PreCreateEntityEvent;
//import company.search.qualifiers.RecruitEvent;
//import company.search.qualifiers.VisitCandidateEvent;
//import facades.CompanyFacade;
//import facades.CompanyFolderFacade;
//import qualifiers.ChosenEntityEvent;
//import qualifiers.EditedEntityEvent;
//import qualifiers.LoggedIn;
//import qualifiers.PreEditEntityEvent;
//import qualifiers.ReportedProfile;
//import sqlgenerators.search.SearchSQLGenerator;
//import sqlgenerators.search.SearchSqlGeneratorInterface;
//
///**
// *
// * @author damfercer
// */
//@Named
//@SessionScoped
//public class ViewSearchBean extends AbstractEntityCreator<CompanyAdquiredProfileFolderFacade,CompanyAdquiredProfileFolder> implements Serializable {
//    @Inject
//    CandidateFacade candidateEJB;
//    @Inject
//    SearchFacade searchEJB;
//    @Inject
//    CompanyAdquiredProfileFacade adquiredEJB;
//    @Inject
//    CompanyVisitedProfileFacade companyVisitedEJB;
//    @Inject
//    CompanyAdquiredProfileFolderFacade companyAdquiredProfileFolderFacade;
//    @Inject
//    @PreCreateEntityEvent
//    Event<CompanyAdquiredProfile> preCreateAdquiredProfileEvent;
//    @Inject
//    @CreatedEntityEvent
//    Event<CompanyAdquiredProfile> createdAdquiredProfileEvent;
//    @Inject
//    CompanyFolderFacade companyFolderFacade;
//    @Inject
//    @VisitCandidateEvent
//    private Event<CompanyVisitedProfile> companyVisitedProfileEvent;
//    @Inject
//    @RecruitEvent
//    private Event<CompanyAdquiredProfile>companyAdquiredProfileEvent;
//    
//    private SearchSqlGeneratorInterface sqlGenerator;
//    private Search currentSearch;
//    private Integer count;
//    private List<Candidate> candidates;
//    private List<Candidate> interestedCandidates;
//    private List<Candidate> adquiredCandidates;
//    private List<Candidate> loadedCandidates;
//    private final Integer numberOfCandidatesPerView=52;
//    private Candidate selectedCandidate;
//    private Candidate selectedAdquiredCandidate;
//    private Integer maxCandidates;
//    @Inject
//    @LoggedIn
//    private Company company;
//    @Inject
//    CompanyAdquiredProfileFacade companyAdquiredProfileFacade;
//    @Inject
//    @RecruitEvent
//    Event<CompanyFolder> recruitEvent;
//    @Inject
//    @SeeAllProfilesEvent
//    Event<Integer> recruitEventNullFolder;
//    private Boolean shouldScroll;
//    private Integer facesCountScroll;
//    @Inject
//    @EditedEntityEvent
//    private Event<Company> editedCompanyEvent;
//    @Inject
//    @PreEditEntityEvent
//    private Event<Company> preEditedCompanyEvent;
//    @Inject
//    @ReportedProfile
//    private Event<Candidate> candidateReported;
//    @Inject
//    private CompanyFacade companyFacade;
//    /**
//     * Creates a new instance of ViewSearchBean
//     */
//    public ViewSearchBean() {
//        shouldScroll=false;
//        sqlGenerator=new SearchSQLGenerator();
//        count=0;
//        facesCountScroll=0;
//        candidates=new ArrayList<Candidate>();
//        interestedCandidates=new ArrayList<Candidate>();
//        adquiredCandidates=new ArrayList<Candidate>();
//        loadedCandidates= new ArrayList<Candidate>();
//    }
//    
//    public void onNewSearch(@Observes @CreatedEntityEvent Search newSearch){
//        facesCountScroll=0;
//        shouldScroll=false;
//        this.currentSearch=newSearch;
//        this.currentSearch.setLastSearchDate(new Date());
//        searchEJB.edit(this.currentSearch);
//        setCount(0);
//        getCandidates().clear();
//        getInterestedCandidates().clear();
//        setCandidates(searchCandidates());
//        setInterestedCandidates(searchInterestedCandidates());
//        this.maxCandidates=queryNumberOfCandidates();
//        this.adquiredCandidates=new ArrayList<Candidate>();
//        loadedCandidates= new ArrayList<Candidate>();
//    }
//    
//    public void onSelectedSearch(@Observes @ChosenEntityEvent Search selectedSearch){
//        facesCountScroll=0;
//        shouldScroll=false;
//        this.currentSearch=selectedSearch;
//        this.currentSearch.setLastSearchDate(new Date());
//        searchEJB.edit(this.currentSearch);
//        setCount(0);
//        getCandidates().clear();
//        getInterestedCandidates().clear();
//        setCandidates(searchCandidates());
//        setInterestedCandidates(searchInterestedCandidates());
//        this.maxCandidates=queryNumberOfCandidates();
//        this.adquiredCandidates=new ArrayList<Candidate>();
//        loadedCandidates= new ArrayList<Candidate>();
//    }
//    
//    public void selectFace(Candidate c){
//        c.setVisits(c.getVisits()+1);
//        candidateEJB.edit(c);
//        CompanyVisitedProfile visitedProfile=new CompanyVisitedProfile();
//        visitedProfile.setIdCandidate(c);
//        visitedProfile.setIdCompany(getCompany());
//        visitedProfile.setVisitDate(new Date());
//        companyVisitedEJB.create(visitedProfile);
//        if(c.getCompanyVisitedProfileList()==null){
//            c.setCompanyVisitedProfileList(new ArrayList<CompanyVisitedProfile>());
//        }
//        c.getCompanyVisitedProfileList().add(visitedProfile);
//        if(getCompany().getCompanyVisitedProfileList()==null){
//            getCompany().setCompanyVisitedProfileList(new ArrayList<CompanyVisitedProfile>());
//        }
//        getCompany().getCompanyVisitedProfileList().add(visitedProfile);
//        companyVisitedProfileEvent.fire(visitedProfile);
//    }
//    
//    public CompanyAdquiredProfile createCompanyAdquiredProfile(Candidate candidate){
//        CompanyAdquiredProfile adquiredProfile=new CompanyAdquiredProfile();
//        Date currentDate=new Date();
//        adquiredProfile.setAdquisitionDate(currentDate);
//        adquiredProfile.setPayDate(currentDate);
//        adquiredProfile.setIsPayed(true);
//        adquiredProfile.setPrice(BigDecimal.ZERO);
//        adquiredProfile.setCandidate(candidate);
//        adquiredProfile.setCompany(currentSearch.getCompany());
//        preCreateAdquiredProfileEvent.fire(adquiredProfile);
//        adquiredEJB.create(adquiredProfile);
//        createdAdquiredProfileEvent.fire(adquiredProfile);
//        return adquiredProfile;
//    }
//    
//    public CompanyAdquiredProfileFolder createCompanyAdquiredProfileFolder(CompanyAdquiredProfile profile){
//        CompanyAdquiredProfileFolder companyAdquiredProfileFolder=new CompanyAdquiredProfileFolder();
//        companyAdquiredProfileFolder.setCompanyAdquiredProfile(profile);
//        companyAdquiredProfileFolder.setCompanyFolder(currentSearch.getFolder());
//        companyAdquiredProfileFolder.setCreationDate(new Date());
//        return createEntity(companyAdquiredProfileFolder);
//    }
//    
//    private void performMemoryUpdate(CompanyAdquiredProfile adquiredProfile, CompanyAdquiredProfileFolder companyAdquiredProfileFolder){
//        if(currentSearch.getFolder()!=null){
//                if(currentSearch.getFolder().getCompanyAdquiredProfileFolderList()==null){
//                    currentSearch.getFolder().setCompanyAdquiredProfileFolderList(new ArrayList<CompanyAdquiredProfileFolder>());
//                }
//                if(companyAdquiredProfileFolder!=null){
//                    if(!currentSearch.getFolder().getCompanyAdquiredProfileFolderList().contains(companyAdquiredProfileFolder)){
//                        currentSearch.getFolder().getCompanyAdquiredProfileFolderList().add(companyAdquiredProfileFolder);
//                    }
//                    if(adquiredProfile.getCompanyAdquiredProfileFolderList()==null){
//                        adquiredProfile.setCompanyAdquiredProfileFolderList(new ArrayList<CompanyAdquiredProfileFolder>());
//                    }
//                    if(!adquiredProfile.getCompanyAdquiredProfileFolderList().contains(companyAdquiredProfileFolder)){
//                        adquiredProfile.getCompanyAdquiredProfileFolderList().add(companyAdquiredProfileFolder);
//                    }
//                       
//                }
//            }
//        if(company.getCompanyAdquiredProfileList()==null){
//            company.setCompanyAdquiredProfileList(new ArrayList<CompanyAdquiredProfile>());
//        }
//        if(!company.getCompanyAdquiredProfileList().contains(adquiredProfile)){
//            company.getCompanyAdquiredProfileList().add(adquiredProfile);
//        }
//    }
//    //TODO: TRANSACTIONAL
//    public String recruit(){
//        for(Candidate adquiredCandidate:adquiredCandidates){
//            CompanyAdquiredProfile adquiredProfile=companyAdquiredProfileFacade.find(adquiredCandidate, company);
//            CompanyAdquiredProfileFolder companyAdquiredProfileFolder=null;
//            if(adquiredProfile==null){
//                adquiredProfile=createCompanyAdquiredProfile(adquiredCandidate);
//                if(currentSearch.getFolder()!=null){
//                    companyAdquiredProfileFolder=createCompanyAdquiredProfileFolder(adquiredProfile);
//                }
//                currentSearch.getCompany().setAvailableCredits(currentSearch.getCompany().getAvailableCredits()-1);   
//            }
//            else{
//                //Preguntamos si el perfil ya existe en esa carpeta. Si no lo metemos
//                if(currentSearch.getFolder()!=null){
//                    companyAdquiredProfileFolder=companyAdquiredProfileFolderFacade.find(adquiredProfile, currentSearch.getFolder());
//                }
//                if(companyAdquiredProfileFolder==null && currentSearch.getFolder()!=null){
//                   companyAdquiredProfileFolder=createCompanyAdquiredProfileFolder(adquiredProfile);
//                }
//            }
//            companyAdquiredProfileEvent.fire(adquiredProfile);
//            performMemoryUpdate(adquiredProfile, companyAdquiredProfileFolder);
//        }
//        preEditedCompanyEvent.fire(currentSearch.getCompany());
//        companyFacade.edit(currentSearch.getCompany());
//        editedCompanyEvent.fire(currentSearch.getCompany());
//        String redirect="/views/company/folder/ViewFolderView?faces-redirect=true";
//        if(currentSearch.getFolder()==null){
//            redirect="/views/company/folder/ViewAllProfilesView?faces-redirect=true";
//        }
//        if(currentSearch.getFolder()!=null){
//            recruitEvent.fire(currentSearch.getFolder());
//        }
//        else{
//            recruitEventNullFolder.fire(0);
//        }
//        
//        //this.currentSearch=null;
//        this.adquiredCandidates=new ArrayList<Candidate>();
//        return redirect;
//    }
//    
//    public void searchMoreCandidates(){
//        shouldScroll=true;
//        loadedCandidates=searchCandidates();
//        getCandidates().addAll(loadedCandidates);
//    }
//    
//    public Integer getNumberOfRows(){
//       return getCandidates().size();
//    }
//    private List<Candidate> searchCandidates(){
//        String sql=sqlGenerator.getSqlFromSearch(currentSearch);
//        List<Candidate> searchCandidates = candidateEJB.getSearchCandidates(sql,count, getNumberOfCandidatesPerView(),getCompany());
//        facesCountScroll=count;
//        count+=getNumberOfCandidatesPerView();
//        return searchCandidates;  
//    }
//    
//    private List<Candidate> searchInterestedCandidates(){
//        String sql=sqlGenerator.getSqlFromSearch(currentSearch);
//        return candidateEJB.getInterestedAndValidCandidatesInASearch(sql,  currentSearch);
//    }
//    
//    private Integer queryNumberOfCandidates(){
//        String sql=sqlGenerator.getSqlFromSearch(currentSearch);
//        Integer number=candidateEJB.getNumberOfValidCandidates(sql,getCompany());
//        return number;
//    }
//    
//    /**
//     * @return the currentSearch
//     */
//    public Search getCurrentSearch() {
//        return currentSearch;
//    }
//    
//    public void adquireSelectedCandidate(){
//        Integer credits=currentSearch.getCompany().getAvailableCredits();
//        if(credits-adquiredCandidates.size()<=0){
//        }
//        else{
//            if(!adquiredCandidates.contains(selectedCandidate)){
//                adquiredCandidates.add(selectedCandidate);
//            }
//        }
//    }
//    
//    public void reportSelectedCandidate(){
//        candidateReported.fire(selectedCandidate);
//    }
//    
//    public void removeSelectedCandidate(){
//        adquiredCandidates.remove(selectedAdquiredCandidate);
//    }
//
//    /**
//     * @param currentSearch the currentSearch to set
//     */
//    public void setCurrentSearch(Search currentSearch) {
//        this.currentSearch = currentSearch;
//    }
//    
//    public Integer dateDifference(Date compare){
//        Date now=new Date();
//        long diff=now.getTime()-compare.getTime();
//        long days=diff / 1000 / 60 / 60 / 24 / 365;
//        return (int)days;
//    }
//    
//    /**
//     * @return the count
//     */
//    public Integer getCount() {
//        return count;
//    }
//
//    /**
//     * @param count the count to set
//     */
//    public void setCount(Integer count) {
//        this.count = count;
//    }
//
//    /**
//     * @return the sqlGenerator
//     */
//    public SearchSqlGeneratorInterface getSqlGenerator() {
//        return sqlGenerator;
//    }
//
//    /**
//     * @param sqlGenerator the sqlGenerator to set
//     */
//    public void setSqlGenerator(SearchSqlGeneratorInterface sqlGenerator) {
//        this.sqlGenerator = sqlGenerator;
//    }
//
//    /**
//     * @return the adquiredCandidates
//     */
//    public List<Candidate> getAdquiredCandidates() {
//        return adquiredCandidates;
//    }
//
//    /**
//     * @param adquiredCandidates the adquiredCandidates to set
//     */
//    public void setAdquiredCandidates(List<Candidate> adquiredCandidates) {
//        this.adquiredCandidates = adquiredCandidates;
//    }
//
//    /**
//     * @return the selectedCadidate
//     */
//    public Candidate getSelectedCandidate() {
//        return selectedCandidate;
//    }
//    
//    /**
//     * @param selectedCadidate the selectedCadidate to set
//     */
//    public void setSelectedCandidate(Candidate selectedCadidate) {
//        this.selectedCandidate = selectedCadidate;
//    }
//
//    /**
//     * @return the candidates
//     */
//    public List<Candidate> getCandidates() {
//        return candidates;
//    }
//
//    /**
//     * @param candidates the candidates to set
//     */
//    public void setCandidates(List<Candidate> candidates) {
//        this.candidates = candidates;
//    }
//
//    /**
//     * @return the selectedAdquiredCandidate
//     */
//    public Candidate getSelectedAdquiredCandidate() {
//        return selectedAdquiredCandidate;
//    }
//
//    /**
//     * @param selectedAdquiredCandidate the selectedAdquiredCandidate to set
//     */
//    public void setSelectedAdquiredCandidate(Candidate selectedAdquiredCandidate) {
//        this.selectedAdquiredCandidate = selectedAdquiredCandidate;
//    }
//
//    /**
//     * @return the maxCandidates
//     */
//    public Integer getMaxCandidates() {
//        return maxCandidates;
//    }
//
//    /**
//     * @param maxCandidates the maxCandidates to set
//     */
//    public void setMaxCandidates(Integer maxCandidates) {
//        this.maxCandidates = maxCandidates;
//    }
//
//    /**
//     * @return the company
//     */
//    public Company getCompany() {
//        return company;
//    }
//
//    /**
//     * @param company the company to set
//     */
//    public void setCompany(Company company) {
//        this.company = company;
//    }
//
//    /**
//     * @return the shouldScroll
//     */
//    public Boolean getShouldScroll() {
//        return shouldScroll;
//    }
//
//    /**
//     * @param shouldScroll the shouldScroll to set
//     */
//    public void setShouldScroll(Boolean shouldScroll) {
//        this.shouldScroll = shouldScroll;
//    }
//
//    /**
//     * @return the facesCountScroll
//     */
//    public Integer getFacesCountScroll() {
//        return facesCountScroll;
//    }
//
//    /**
//     * @param facesCountScroll the facesCountScroll to set
//     */
//    public void setFacesCountScroll(Integer facesCountScroll) {
//        this.facesCountScroll = facesCountScroll;
//    }
//    
//    public Boolean isAdquiredCandidate(Candidate c){
//        return companyAdquiredProfileFacade.find(c, company)!=null;
//    }
//
//    /**
//     * @return the loadedCandidates
//     */
//    public List<Candidate> getLoadedCandidates() {
//        return loadedCandidates;
//    }
//
//    /**
//     * @param loadedCandidates the loadedCandidates to set
//     */
//    public void setLoadedCandidates(List<Candidate> loadedCandidates) {
//        this.loadedCandidates = loadedCandidates;
//    }
//
//    /**
//     * @return the numberOfCandidatesPerView
//     */
//    public Integer getNumberOfCandidatesPerView() {
//        return numberOfCandidatesPerView;
//    }
//
//    /**
//     * @return the interestedCandidates
//     */
//    public List<Candidate> getInterestedCandidates() {
//        return interestedCandidates;
//    }
//
//    /**
//     * @param interestedCandidates the interestedCandidates to set
//     */
//    public void setInterestedCandidates(List<Candidate> interestedCandidates) {
//        this.interestedCandidates = interestedCandidates;
//    }
//
//
//
//}
